using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BusinessObjects;

namespace GUI
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

        }
        WMIManager insWMIManager = new WMIManager();
        ChameleonManager insChameleonManager = new ChameleonManager();
        private void frmMain_Load(object sender, EventArgs e)
        {
            

            GenerateMenu();


        }

        private void cmsChameleon_Opening(object sender, CancelEventArgs e)
        {
            GenerateMenu();
        }

        private void GenerateMenu()
        {
            cmsChameleon.Items.Clear();
            foreach (Connection c in insWMIManager.GetConnections())
            {
                ToolStripMenuItem insToolStripMenuItem = new ToolStripMenuItem(); ;
                insToolStripMenuItem.Text = c.ConnectionName;
                insToolStripMenuItem.Tag = c;

                foreach (Profile p in insChameleonManager.GetProfiles())
                {
                    ToolStripMenuItem insToolStripMenuItemSub = new ToolStripMenuItem();
                    insToolStripMenuItemSub.Text = p.ProfileName;
                    insToolStripMenuItemSub.Tag = p;
                    insToolStripMenuItemSub.Click += new EventHandler(insToolStripMenuItemSub_Click);
                    insToolStripMenuItem.DropDownItems.Add(insToolStripMenuItemSub);
                }

                ToolStripMenuItem insToolStripMenuItemSubAutomatic = new ToolStripMenuItem();
                insToolStripMenuItemSubAutomatic.Text = "Use Automatic";
                insToolStripMenuItemSubAutomatic.Tag = null;
                insToolStripMenuItemSubAutomatic.Click += new EventHandler(insToolStripMenuItemSub_Click);
                insToolStripMenuItem.DropDownItems.Add(insToolStripMenuItemSubAutomatic);

                cmsChameleon.Items.Add(insToolStripMenuItem);
            }

            ToolStripSeparator insToolStripSeparator1 = new ToolStripSeparator();
            cmsChameleon.Items.Add(insToolStripSeparator1);

            ToolStripMenuItem insToolStripMenuItemProfiles = new ToolStripMenuItem();
            insToolStripMenuItemProfiles.Text = "Profiles";
            insToolStripMenuItemProfiles.Click += delegate { frmProfiles insFrmProfiles = new frmProfiles(); insFrmProfiles.ShowDialog(); };
            cmsChameleon.Items.Add(insToolStripMenuItemProfiles);

            ToolStripSeparator insToolStripSeparator2 = new ToolStripSeparator();
            cmsChameleon.Items.Add(insToolStripSeparator2);

            ToolStripMenuItem insToolStripMenuItemHelp = new ToolStripMenuItem();
            insToolStripMenuItemHelp.Text = "Help";
            cmsChameleon.Items.Add(insToolStripMenuItemHelp);

            ToolStripMenuItem insToolStripMenuItemAbout = new ToolStripMenuItem();
            insToolStripMenuItemAbout.Text = "About";
            insToolStripMenuItemAbout.Click += delegate { frmAbout insFrmAbout = new frmAbout(); insFrmAbout.ShowDialog(); };
            insToolStripMenuItemHelp.DropDownItems.Add(insToolStripMenuItemAbout);

            ToolStripMenuItem insToolStripMenuItemRegister = new ToolStripMenuItem();
            insToolStripMenuItemRegister.Text = "Subscribe For Updates";
            insToolStripMenuItemRegister.Click += delegate { frmRegister insFrmRegister = new frmRegister(); insFrmRegister.ShowDialog(); };
            insToolStripMenuItemHelp.DropDownItems.Add(insToolStripMenuItemRegister);

            ToolStripSeparator insToolStripSeparator3 = new ToolStripSeparator();
            cmsChameleon.Items.Add(insToolStripSeparator3);

            ToolStripMenuItem insToolStripMenuItemExit = new ToolStripMenuItem();
            insToolStripMenuItemExit.Text = "Exit";
            insToolStripMenuItemExit.Click += delegate { Application.Exit(); };
            cmsChameleon.Items.Add(insToolStripMenuItemExit);

        }

        void insToolStripMenuItemSub_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem insToolStripMenuItem = (ToolStripMenuItem)sender;
            Profile p = (Profile)insToolStripMenuItem.Tag;
            Connection c = (Connection)insToolStripMenuItem.OwnerItem.Tag;
            if (p == null)
            {
                insWMIManager.SetConnectionAutomatic(c);
            }
            else
            {
                insWMIManager.SetConnectionProfile(c, p);
            }

        }

        private void frmMain_Activated(object sender, EventArgs e)
        {
            this.Hide();
            this.Visible = false;
            this.niChameleon.Visible = true;
        }
    }
}